<?php
//自动生成,请根据需要修改
namespace app\mqtt\controller;
use app\common\controller\Base;
use think\facade\Request;
use think\Db;

/**
* @title 账户信息接口
* @description 账户信息说明
* @group mqtt
*/
class AccountMsg extends Base{

	/**
	* @title 插入记录
	* @description 插入账户信息数据
	* @author 开发者（范浩婷）
	* @url /mqtt/account_msg/add
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:msg_type type:VarChar require:1 default:0 other: desc:消息类型（普通文件,语言,视频,图片,链接）
	* @param name:is_sended type:TinyInt require:1 default:0 other: desc:已经发送
	* @param name:accept_time type:Int require:1 default:0 other: desc:接收时间
	* @param name:is_accepted type:TinyInt require:1 default:0 other: desc:已接受
	* @param name:file_url type:VarChar require:1 default:0 other: desc:文件路径
	* @param name:accept_account_id type:VarChar require:1 default:0 other: desc:接收用户
	* @param name:file_type type:VarChar require:1 default:0 other: desc:文件类型(普通文件,语言,视频,图片,链接)
	* @param name:file_size type:VarChar require:1 default:0 other: desc:文件大小
	* @param name:file_name type:VarChar require:1 default:0 other: desc:文件名
	* @param name:is_file type:TinyInt require:1 default:0 other: desc:是文件
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:content type:TinyInt require:1 default:0 other: desc:内容
	* @param name:send_account_id type:Int require:1 default:0 other: desc:发送账号id
	* @param name:msg_id type:VarChar require:1 default:0 other: desc:文件信息id
	*
	*
	* @return msg_type:普通文件
	* @return is_sended:已经发送
	* @return accept_time:接收时间
	* @return is_accepted:已接受
	* @return file_url:文件路径
	* @return accept_account_id:接收用户
	* @return file_type:文件类型
	* @return file_size:文件大小
	* @return file_name:文件名
	* @return is_file:是文件
	* @return chat_id:窗口id
	* @return create_time:创建时间
	* @return content:内容
	* @return send_account_id:发送账号id
	* @return msg_id:文件信息id
	*
	*/
	public function add (){
		//模块 - 输入数据
		$input=input();

		//模块 - 验证器
		$account_msgValidate = new \app\mqtt\validate\AccountMsg;
		$validate = $account_msgValidate->scene('except_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $account_msgValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 新增  ---
		//插入的数据
		$insert_data = [
			'msg_type'=>$input['msg_type'],
			'is_sended'=>$input['is_sended'],
			'accept_time'=>$input['accept_time'],
			'is_accepted'=>$input['is_accepted'],
			'file_url'=>$input['file_url'],
			'accept_account_id'=>$input['accept_account_id'],
			'file_type'=>$input['file_type'],
			'file_size'=>$input['file_size'],
			'file_name'=>$input['file_name'],
			'is_file'=>$input['is_file'],
			'chat_id'=>$input['chat_id'],
			'create_time'=>$input['create_time'],
			'content'=>$input['content'],
			'send_account_id'=>$input['send_account_id'],
			'msg_id'=>$input['msg_id'],
		];
		//添加数据
		$insert_res = Db::name('account_msg')->data($insert_data)->insert();
		if(!$insert_res){
			$msg['msg'] = '插入数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $insert_res;

		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（paginate）
	* @description 获取账户信息的列表数据（分页）
	* @author 开发者（范浩婷）
	* @url /mqtt/account_msg/get_list
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:msg_type type:VarChar require:1 default:0 other: desc:消息类型（普通文件,语言,视频,图片,链接）
	* @param name:is_sended type:TinyInt require:1 default:0 other: desc:已经发送
	* @param name:accept_time type:Int require:1 default:0 other: desc:接收时间
	* @param name:is_accepted type:TinyInt require:1 default:0 other: desc:已接受
	* @param name:file_url type:VarChar require:1 default:0 other: desc:文件路径
	* @param name:accept_account_id type:VarChar require:1 default:0 other: desc:接收用户
	* @param name:file_type type:VarChar require:1 default:0 other: desc:文件类型(普通文件,语言,视频,图片,链接)
	* @param name:file_size type:VarChar require:1 default:0 other: desc:文件大小
	* @param name:file_name type:VarChar require:1 default:0 other: desc:文件名
	* @param name:is_file type:TinyInt require:1 default:0 other: desc:是文件
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:content type:TinyInt require:1 default:0 other: desc:内容
	* @param name:send_account_id type:Int require:1 default:0 other: desc:发送账号id
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:msg_id type:VarChar require:1 default:0 other: desc:文件信息id
	*
	* @param name:page type:Int require:0 default:0 other: desc:当前页
	* @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
	*
	* @return msg_type:普通文件
	* @return is_sended:已经发送
	* @return accept_time:接收时间
	* @return is_accepted:已接受
	* @return file_url:文件路径
	* @return accept_account_id:接收用户
	* @return file_type:文件类型
	* @return file_size:文件大小
	* @return file_name:文件名
	* @return is_file:是文件
	* @return chat_id:窗口id
	* @return create_time:创建时间
	* @return content:内容
	* @return send_account_id:发送账号id
	* @return id:id
	* @return msg_id:文件信息id
	*
	*/
	public function get_list (){
		//模块 - 输入数据
        $input=input();

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['msg_type','like','%'.$input['msg_type'].'%'],
			['is_sended','=',$input['is_sended']],
			['accept_time','=',$input['accept_time']],
			['is_accepted','=',$input['is_accepted']],
			['file_url','like','%'.$input['file_url'].'%'],
			['accept_account_id','like','%'.$input['accept_account_id'].'%'],
			['file_type','like','%'.$input['file_type'].'%'],
			['file_size','like','%'.$input['file_size'].'%'],
			['file_name','like','%'.$input['file_name'].'%'],
			['is_file','=',$input['is_file']],
			['chat_id','=',$input['chat_id']],
			['create_time','=',$input['create_time']],
			['content','=',$input['content']],
			['send_account_id','=',$input['send_account_id']],
			['id','=',$input['id']],
			['msg_id','like','%'.$input['msg_id'].'%'],
		];
		//去除为空的条件
		$paginate_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($paginate_map,$v);
		}

		//获取数据
		$select_res = Db::name('account_msg')->where($paginate_map)->paginate();

		// paginate 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（select）
	* @description 获取账户信息的列表数据（无分页）
	* @author 开发者（范浩婷）
	* @url /mqtt/account_msg/get_select
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:msg_type type:VarChar require:1 default:0 other: desc:消息类型（普通文件,语言,视频,图片,链接）
	* @param name:is_sended type:TinyInt require:1 default:0 other: desc:已经发送
	* @param name:accept_time type:Int require:1 default:0 other: desc:接收时间
	* @param name:is_accepted type:TinyInt require:1 default:0 other: desc:已接受
	* @param name:file_url type:VarChar require:1 default:0 other: desc:文件路径
	* @param name:accept_account_id type:VarChar require:1 default:0 other: desc:接收用户
	* @param name:file_type type:VarChar require:1 default:0 other: desc:文件类型(普通文件,语言,视频,图片,链接)
	* @param name:file_size type:VarChar require:1 default:0 other: desc:文件大小
	* @param name:file_name type:VarChar require:1 default:0 other: desc:文件名
	* @param name:is_file type:TinyInt require:1 default:0 other: desc:是文件
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:content type:TinyInt require:1 default:0 other: desc:内容
	* @param name:send_account_id type:Int require:1 default:0 other: desc:发送账号id
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:msg_id type:VarChar require:1 default:0 other: desc:文件信息id
	*
	*
	* @return msg_type:普通文件
	* @return is_sended:已经发送
	* @return accept_time:接收时间
	* @return is_accepted:已接受
	* @return file_url:文件路径
	* @return accept_account_id:接收用户
	* @return file_type:文件类型
	* @return file_size:文件大小
	* @return file_name:文件名
	* @return is_file:是文件
	* @return chat_id:窗口id
	* @return create_time:创建时间
	* @return content:内容
	* @return send_account_id:发送账号id
	* @return id:id
	* @return msg_id:文件信息id
	*
	*/
	public function get_select (){
		//模块 - 输入数据
        $input=input();

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['msg_type','like','%'.$input['msg_type'].'%'],
			['is_sended','=',$input['is_sended']],
			['accept_time','=',$input['accept_time']],
			['is_accepted','=',$input['is_accepted']],
			['file_url','like','%'.$input['file_url'].'%'],
			['accept_account_id','like','%'.$input['accept_account_id'].'%'],
			['file_type','like','%'.$input['file_type'].'%'],
			['file_size','like','%'.$input['file_size'].'%'],
			['file_name','like','%'.$input['file_name'].'%'],
			['is_file','=',$input['is_file']],
			['chat_id','=',$input['chat_id']],
			['create_time','=',$input['create_time']],
			['content','=',$input['content']],
			['send_account_id','=',$input['send_account_id']],
			['id','=',$input['id']],
			['msg_id','like','%'.$input['msg_id'].'%'],
		];
		//去除为空的条件
		$select_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($select_map,$v);
		}

		//获取数据
		$select_res = Db::name('account_msg')->where($select_map)->select();

		// select 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取单条数据
	* @description 根据ID获取账户信息的指定记录
	* @author 开发者（范浩婷）
	* @url /mqtt/account_msg/info
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	*
	*
	* @return id:id
	*
	*/
	public function info (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');

		// input结果返回
		$msg['data']['id'] = $input['id'];

		//模块 - 验证器
		$account_msgValidate = new \app\mqtt\validate\AccountMsg;
		$validate = $account_msgValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $account_msgValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}

		//获取数据
		$select_res = Db::name('account_msg')->where($find_map)->find();

		// find 返回数据
		$msg['data'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 更新记录
	* @description 根据ID更新账户信息指定记录的数据
	* @author 开发者（范浩婷）
	* @url /mqtt/account_msg/edit
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:msg_type type:VarChar require:1 default:0 other: desc:消息类型（普通文件,语言,视频,图片,链接）
	* @param name:is_sended type:TinyInt require:1 default:0 other: desc:已经发送
	* @param name:accept_time type:Int require:1 default:0 other: desc:接收时间
	* @param name:is_accepted type:TinyInt require:1 default:0 other: desc:已接受
	* @param name:file_url type:VarChar require:1 default:0 other: desc:文件路径
	* @param name:accept_account_id type:VarChar require:1 default:0 other: desc:接收用户
	* @param name:file_type type:VarChar require:1 default:0 other: desc:文件类型(普通文件,语言,视频,图片,链接)
	* @param name:file_size type:VarChar require:1 default:0 other: desc:文件大小
	* @param name:file_name type:VarChar require:1 default:0 other: desc:文件名
	* @param name:is_file type:TinyInt require:1 default:0 other: desc:是文件
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:content type:TinyInt require:1 default:0 other: desc:内容
	* @param name:send_account_id type:Int require:1 default:0 other: desc:发送账号id
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:msg_id type:VarChar require:1 default:0 other: desc:文件信息id
	*
	*
	* @return msg_type:普通文件
	* @return is_sended:已经发送
	* @return accept_time:接收时间
	* @return is_accepted:已接受
	* @return file_url:文件路径
	* @return accept_account_id:接收用户
	* @return file_type:文件类型
	* @return file_size:文件大小
	* @return file_name:文件名
	* @return is_file:是文件
	* @return chat_id:窗口id
	* @return create_time:创建时间
	* @return content:内容
	* @return send_account_id:发送账号id
	* @return id:id
	* @return msg_id:文件信息id
	*
	*/
	public function edit (){
		//模块 - 输入数据
        $input=input();

		//模块 - 验证器
		$account_msgValidate = new \app\mqtt\validate\AccountMsg;
		$validate = $account_msgValidate->scene('all_field')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $account_msgValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 修改  ---
		//更新的条件
		$update_date = [
		'id'=>$input['id'],
		];
		//更新的数据
		$update_map = [
			'msg_type'=>$input['msg_type'],
			'is_sended'=>$input['is_sended'],
			'accept_time'=>$input['accept_time'],
			'is_accepted'=>$input['is_accepted'],
			'file_url'=>$input['file_url'],
			'accept_account_id'=>$input['accept_account_id'],
			'file_type'=>$input['file_type'],
			'file_size'=>$input['file_size'],
			'file_name'=>$input['file_name'],
			'is_file'=>$input['is_file'],
			'chat_id'=>$input['chat_id'],
			'create_time'=>$input['create_time'],
			'content'=>$input['content'],
			'send_account_id'=>$input['send_account_id'],
			'msg_id'=>$input['msg_id'],
		];
		//获取数据
		$update_res = Db::name('account_msg')->where($update_date)->update($update_map);
		if(!$update_res){
			$msg['msg'] = '更新据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $update_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 按条件删除记录
	* @description 按条件删除账户信息的数据
	* @author 开发者（范浩婷）
	* @url /mqtt/account_msg/delete
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:msg_type type:VarChar require:1 default:0 other: desc:消息类型（普通文件,语言,视频,图片,链接）
	* @param name:is_sended type:TinyInt require:1 default:0 other: desc:已经发送
	* @param name:accept_time type:Int require:1 default:0 other: desc:接收时间
	* @param name:is_accepted type:TinyInt require:1 default:0 other: desc:已接受
	* @param name:file_url type:VarChar require:1 default:0 other: desc:文件路径
	* @param name:accept_account_id type:VarChar require:1 default:0 other: desc:接收用户
	* @param name:file_type type:VarChar require:1 default:0 other: desc:文件类型(普通文件,语言,视频,图片,链接)
	* @param name:file_size type:VarChar require:1 default:0 other: desc:文件大小
	* @param name:file_name type:VarChar require:1 default:0 other: desc:文件名
	* @param name:is_file type:TinyInt require:1 default:0 other: desc:是文件
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	* @param name:create_time type:Int require:1 default:0 other: desc:创建时间
	* @param name:content type:TinyInt require:1 default:0 other: desc:内容
	* @param name:send_account_id type:Int require:1 default:0 other: desc:发送账号id
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:msg_id type:VarChar require:1 default:0 other: desc:文件信息id
	*
	*
	* @return msg_type:普通文件
	* @return is_sended:已经发送
	* @return accept_time:接收时间
	* @return is_accepted:已接受
	* @return file_url:文件路径
	* @return accept_account_id:接收用户
	* @return file_type:文件类型
	* @return file_size:文件大小
	* @return file_name:文件名
	* @return is_file:是文件
	* @return chat_id:窗口id
	* @return create_time:创建时间
	* @return content:内容
	* @return send_account_id:发送账号id
	* @return id:id
	* @return msg_id:文件信息id
	*
	*/
	public function delete (){
		//模块 - 输入数据
        $input=input();

		// ---  模块 - 删除  ---
		//删除的条件
		$temp_map = [
			['msg_type','=',$input['msg_type']],
			['is_sended','=',$input['is_sended']],
			['accept_time','=',$input['accept_time']],
			['is_accepted','=',$input['is_accepted']],
			['file_url','=',$input['file_url']],
			['accept_account_id','=',$input['accept_account_id']],
			['file_type','=',$input['file_type']],
			['file_size','=',$input['file_size']],
			['file_name','=',$input['file_name']],
			['is_file','=',$input['is_file']],
			['chat_id','=',$input['chat_id']],
			['create_time','=',$input['create_time']],
			['content','=',$input['content']],
			['send_account_id','=',$input['send_account_id']],
			['id','=',$input['id']],
			['msg_id','=',$input['msg_id']],
		];
		$delete_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2] == null || $v[2] == ''){
				continue;
			}
			array_push($delete_map,$v);
		}
		if(empty($delete_map)){
			$msg['msg'] = '删除条件不能为空';
			return $this->errorJson($msg);
		}

		//删除数据
		$delete_res = Db::name('account_msg')->where($delete_map)->delete();
		if(!$delete_res){
			$msg['msg'] = '删除数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $delete_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
    /**
     * @title 获取未读消息列表
     * @description  按条件获取未读消息列表
     * @author 开发者（袁丽蓉）
     * @url /mqtt/account_msg/get_unread_message
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
     *
     * @param name:send_account_id type:Int require:1 default:0 other: desc:发送者id
     * @param name:send_account_type type:Varchar require:1 default:0 other: desc:发送者类型
     *
     * @param name:accept_account_id type:Varchar require:1 default:0 other: desc:接收者id
     * @param name:accept_account_type type:Varchar require:1 default:0 other: desc:接收者类型
     *
     *
     *
     * @return chat_id:chat_id
     *
     * @return accept_account_id:accept_account_id
     * @return send_account_id:send_account_id
     * @return accept_account_type:accept_account_type
     * @return send_account_type:send_account_type
     *
     */
    public function   get_unread_message(){
        //模块 - 输入数据
        $data = input();

//        //模块 - 验证器
//        $validate=new Validate();
//        $validate->rule([
//            'chat_id'=>'number',
//            'send_account_id'=>'require',
//            'send_account_type'=>'require',
//            'accept_account_id'=>'require',
//            'accept_account_type'=>'require',
//
//        ]);
//        if (!$validate->check($data)) {
//            $msg['msg'] = '验证失败';
//            $msg['validate'] = $validate->getError();
//            return $this->errorJson($msg);
//        }

        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['chat_id','=',$data['chat_id']],
            ['send_account_id','=',$data['send_account_id']],
            ['send_account_type','=',$data['send_account_type']],
            ['accept_account_id','=',$data['accept_account_id']],
            ['accept_account_type','=',$data['accept_account_type']],

        ];
        //去除为空的条件
        $find_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($find_map,$v);
        }

        //获取数据
        $select_res = Db::name('account_msg')
            ->where($find_map)
            ->where('is_accepted',0)
            ->select();

        $num = Db::name('account_msg')
            ->where($find_map)
            ->where('is_accepted',0)
            ->count();

        // select 返回数据
        $msg['data']['num'] = $num;
        $msg['data']['list'] = $select_res;

        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
    /**
     * @title 获取已读消息列表
     * @description  按条件获取已读消息列表
     * @author 开发者（范浩婷）
     * @url /mqtt/account_msg/get_read_message
     * @method *
     *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
     *
     * @param name:send_account_id type:Int require:1 default:0 other: desc:发送者id
     * @param name:send_account_type type:Varchar require:1 default:0 other: desc:发送者类型
     *
     * @param name:accept_account_id type:Varchar require:1 default:0 other: desc:接收者id
     * @param name:accept_account_type type:Varchar require:1 default:0 other: desc:接收者类型
     *
     *
     *
     * @return chat_id:chat_id
     *
     * @return accept_account_id:accept_account_id
     * @return send_account_id:send_account_id
     * @return accept_account_type:accept_account_type
     * @return send_account_type:send_account_type
     *
     */
    public function   get_read_message(){
        //模块 - 输入数据
        $data = input();

        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['chat_id','=',$data['chat_id']],
            ['send_account_id','=',$data['send_account_id']],
            ['send_account_type','=',$data['send_account_type']],
            ['accept_account_id','=',$data['accept_account_id']],
            ['accept_account_type','=',$data['accept_account_type']],

        ];
        //去除为空的条件
        $find_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($find_map,$v);
        }

        //获取数据
        $select_res = Db::name('account_msg')
            ->where($find_map)
            ->where('is_accepted',1)
            ->select();

        // find 返回数据
        $msg['data'] = $select_res;

        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }

}
